//------------------------------------------------------------------------------
// The contents of this file are subject to the Microsoft Public License (Ms-PL)
// You may obtain a copy of the License at http://psadmin.codeplex.com/license. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, 
// WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations 
// under the License.
// 
// The initial developer is Ben Foster (ben.foster@retroviz.com)
// Copyright (c) Retroviz Limited (http://www.retroviz.com).
// All Rights Reserved.
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using PSAdmin.Web.Infrastructure;
using PSAdmin.Core.Interfaces;
using PSAdmin.Core.Security;
using System.Web.Security;

namespace PSAdmin.Web.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class SecurityController : Controller
    {
        private IUserRoleRepository _repository;

        public SecurityController(IUserRoleRepository repository) {
            _repository = repository;
        }

        public ActionResult Index() {
            var roles = _repository.GetAll();
            return View(roles);
        }

        [HttpGet]
        public ActionResult EditRole(Guid id) {
            var role = _repository.GetById(id);
            if (role == null) return RedirectToAction("Index");
            return View(role);
        }

        [HttpPost]
        public ActionResult EditRole([Bind(Include = "Name")]UserRole role) {
            if (ModelState.IsValid) {
                _repository.Save(role);
            }
            return View(role);
        }

        [HttpPost]
        public ActionResult DeleteRole(Guid id) {
            var role = _repository.GetById(id);
            if (role != null) {
                _repository.Delete(role);
            }
            return RedirectToAction("Index");                
        }

        [HttpPost]
        public ActionResult AddUserToRole(Guid id, string username) {
            if (!string.IsNullOrEmpty(username))
            {
                var role = _repository.GetById(id);
                if (role != null)
                {
                    role.Users.Add(new UserRoleMapping(username.ToLower()));
                    _repository.Save(role);
                }
            }

            return RedirectToAction("EditRole", new { id = id });
        }

        [HttpPost]
        public JsonResult RemoveUserFromRole(Guid id, Guid userId) {
            bool result = false;
            var role = _repository.GetById(id);

            if (role != null)
            {
                var user = role.Users.FirstOrDefault(x => x.Id == userId);
                if (user != null)
                {
                    role.Users.Remove(user);
                    _repository.Save(role);
                    result = true;
                }
            }
            return Json(result);
        }

        [HttpGet]
        public ActionResult CreateRole() {
            return View();
        }

        [HttpPost]
        public ActionResult CreateRole([Bind(Exclude = "Id")]UserRole role) {
            if (ModelState.IsValid) {
                _repository.Save(role);
                return RedirectToAction("EditRole", new { id = role.Id });
            }
            return View(role);
        }
    }
}
